<?php

namespace App\Modules\LuckDraw\User;

use App\Http\Model\PropLog;
use App\Modules\LuckDraw\Models\AwardLogModel;
use App\Modules\LuckDraw\Models\DrawLogModel;
use App\Modules\LuckDraw\Models\LuckDrawModel;
use App\Modules\LuckDraw\Models\UserPropLogModel;
use App\Modules\LuckDraw\Models\UserPropModel;
use const App\Modules\LuckDraw\Models\DRAW_LOG_FAILED;
use const App\Modules\LuckDraw\Models\DRAW_LOG_SENT;

class LuckUserBase {
    protected $luckId;
    protected $roomId;
    protected $user;
    protected function __construct(){
    }

    public function luckId(){
        return $this->luckId;
    }

    public function userId(){
        return $this->user->user_id;
    }

    public function nickname(){
        return $this->user->nickname;
    }

    public function roomId(){
        return $this->roomId;
    }

    //扣除用户$num资产， 增加已用$num数量
    public function deductProp($num, int $type = UserPropLogModel::USER_PROP_TYPE_DRAW ): int {
        $ok = UserPropModel::deduct($this->luckId, $this->userId(), $num);
        if ($ok > 0) {
            $ok = UserPropLogModel::log($this->luckId, $this->userId(), $type, -$num);
        }
        return $ok;
    }

    //增加$num个道具， 增加已用$num数量 //1.钻石兑换 2.活动赠送
    public function addProp(int $num, int $type = UserPropLogModel::USER_PROP_TYPE_EXCHANGE): bool {
        $ok =  UserPropModel::add($this->luckId, $this->userId(), $num);
        //道具保存成功则增加日志
        if ($ok) {
            $ok = UserPropLogModel::log($this->luckId, $this->userId(),$type, $num);
        }
        return $ok;
    }

    //获取用户资产
    public function getProp() : int {
        return UserPropModel::getAvail($this->luckId, $this->userId());
    }

    //保存奖品
    public function saveAwards(array $awards): bool{
        foreach ($awards as $award) {
            $ok = $award->save();
            if (!$ok) {
                return false;
            }
        }
        return true;
    }

    public function drawLogFailed($num, $rewards, $personal, $strategy, $desc) : int {
        return DrawLogModel::log(
            $this->luckId,
            $this->userId(),
            $personal,
            $strategy,
            $num,
            json_encode($rewards),
            DRAW_LOG_FAILED,
            $desc
        );
    }

    public function drawLogSucess($num, $awards, $poolId, $strategy, $desc) : int {
        $awardArr = [];
        foreach ($awards as $award) {
            array_push(
                $awardArr,
                [
                    'id' => $award->awardId,
                    'type' => $award->type,
                    'price' => $award->price,
                    'time' => $award->timeLimit,
                    'num' => $award->num,
                ]
            );
        }
        return DrawLogModel::log(
            $this->luckId,
            $this->userId(),
            $this->roomId??0,
            $poolId,
            $strategy,
            $num,
            json_encode($awardArr),
            DRAW_LOG_SENT,
            $desc
        );
    }

    public function logAwards(array $awards, int $drawId, int $poolId, string $strategy) : int {
        foreach ($awards as $award) {
            AwardLogModel::log(
                $this->luckId,
                $drawId,
                $this->userId(),
                $poolId,
                $strategy,
                $award->awardId,
                $award->type,
                $award->num,
                $award->price,
                $award->timeLimit
            );
        }
        return true;
    }

    //道具是否足够
    public function propEnough($num) : bool{
        return $this->getProp() >= $num;
    }

    //幸运值是否足够
    public function expEnough(int $expId, int $expNeed): bool {
        if ($expId <= 0) {
            return true;
        }
        return $this->getUserExpNum($expId) >= $expNeed;
    }

    public function record(int $page_num = 0, int $page_size = 20): ?array {
        $offset = ($page_num-1) * $page_size;
        return AwardLogModel::query()
            ->where("luck_id", $this->luckId())
            ->where("user_id", $this->userId())
            ->orderBy('created_at', 'desc')
            ->offset($offset)
            ->limit($page_size)
            ->get()
            ->all();
    }
}